/// tags: BIT SegTree
#include <algorithm>
#include <cstdio>
#include <iostream>
#define lowbit(x) ((x) & -(x))

using namespace std;

namespace BlueQuantum {

int const N = 2e4 + 5;

int n, m, tot, ans;
int h[N], hash[N], rt[N];

struct Node {
  int ls, rs, sum;
} tr[N * 100];

void modify(int &x, int p, int v, int l = 1, int r = *hash) {
  if (!x) x = ++tot;
  tr[x].sum += v;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (p <= mid) modify(tr[x].ls, p, v, l, mid);
  else
    modify(tr[x].rs, p, v, mid + 1, r);
}

int query(int x, int L, int R, int l = 1, int r = *hash) {
  if (!x) return 0;
  if (L <= l && r <= R) return tr[x].sum;
  int mid = (l + r) >> 1, res = 0;
  if (L <= mid) res += query(tr[x].ls, L, R, l, mid);
  if (mid < R) res += query(tr[x].rs, L, R, mid + 1, r);
  return res;
}

int queryR(int x, int y, int L, int R) {
  int res = 0;
  if (x)
    for (int i = x; i; i -= lowbit(i)) res -= query(rt[i], L, R);
  for (int i = y; i; i -= lowbit(i)) res += query(rt[i], L, R);
  return res;
}

inline void add(int x) {
  ans += queryR(0, x - 1, h[x] + 1, *hash) + queryR(x, n, 1, h[x] - 1);
  for (int i = x; i <= n; i += lowbit(i)) modify(rt[i], h[x], 1);
}

inline void del(int x) {
  ans -= queryR(0, x - 1, h[x] + 1, *hash) + queryR(x, n, 1, h[x] - 1);
  for (int i = x; i <= n; i += lowbit(i)) modify(rt[i], h[x], -1);
}

inline void swap(int a, int b) {
  del(a), del(b);
  std::swap(h[a], h[b]);
  add(a), add(b);
}

inline int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i];
    hash[i] = h[i];
  }
  sort(hash + 1, hash + n + 1);
  *hash = unique(hash + 1, hash + n + 1) - hash - 1;
  for (int i = 1; i <= n; ++i)
    h[i] = lower_bound(hash + 1, hash + *hash + 1, h[i]) - hash;
  for (int i = 1; i <= n; ++i) { add(i); }
  cout << ans << '\n';
  cin >> m;
  for (int a, b; m; --m) {
    cin >> a >> b;
    swap(a, b);
    cout << ans << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P1975 [国家集训队] 排队.in", "r", stdin);
  freopen("P1975 [国家集训队] 排队.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
